/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.entity.accessor.entity.IChildAccessor;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.AccessorClassGenContext;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;

//public abstract IChildAccessor createinstance();
public class AccessorCreateInstaceGenerator extends ClassMethodGenerator
{
	private MediateType accessorType;
	private AccessorClassGenContext rootContext;
	private String parentIdPropName;
	private String objectCode;
	private static final String ParentVarName = "Parent";
	private MediateType entityDataType(){return rootContext.getEntityDataGenContext().getProjContext().getEntityDataMediateTypes().get(objectCode);}

	public AccessorCreateInstaceGenerator(MediateType accessorType, AccessorClassGenContext rootContext, String parentIdPropName, String objectCode)
	{
		this.accessorType = accessorType;
		this.rootContext = rootContext;
		this.objectCode = objectCode;
		this.parentIdPropName = parentIdPropName;
	}

	@Override
	protected String getMethodName() {return  "createInstance";}

	@Override
	protected TypeInfo getReturnType() {
		return new TypeInfo(IChildAccessor.class.getTypeName());
	}

	@Override
	protected boolean getIsOverride() {
		return true;
	}
	@Override
	protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
		ArrayList<Modifier.ModifierKeyword> list =new ArrayList<>();
		list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
		return list;
	}

	@Override
	protected ArrayList<ParameterInfo> getParameterCollection() {
		return null;
	}

	@Override
	protected Block buildMethodBody() {
		Block block=ast.newBlock();
		addNewItemDataStatement(block);
		addNewParentIDStatement(block);
		addAssParentIDStatement(block);
		addSetDataParentIDStatement(block);
		addReturnStatement(block);
		return block;
	}

	private void addNewItemDataStatement(Block block) {
		VariableDeclarationFragment variableDeclarationFragment=ast.newVariableDeclarationFragment();
		variableDeclarationFragment.setName(ast.newSimpleName("data"));

		ClassInstanceCreation classInstanceCreation=ast.newClassInstanceCreation();

		classInstanceCreation.setType(ast.newSimpleType(ast.newName(entityDataType().getFullName())));
		variableDeclarationFragment.setInitializer(classInstanceCreation);


		VariableDeclarationStatement variableDeclarationStatement=ast.newVariableDeclarationStatement(variableDeclarationFragment);
		variableDeclarationStatement.setType(ast.newSimpleType(ast.newName(entityDataType().getFullName())));

		block.statements().add(variableDeclarationStatement);
	}

	private void addNewParentIDStatement(Block block) {
		VariableDeclarationFragment variableDeclarationFragment=ast.newVariableDeclarationFragment();
		variableDeclarationFragment.setName(ast.newSimpleName("parentID"));

		StringLiteral stringLiteral=ast.newStringLiteral();
		stringLiteral.setLiteralValue("");
		variableDeclarationFragment.setInitializer(stringLiteral);

		VariableDeclarationStatement variableDeclarationStatement=ast.newVariableDeclarationStatement(variableDeclarationFragment);
		variableDeclarationStatement.setType(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(String.class).getType(ast));
		block.statements().add(variableDeclarationStatement);
	}

	private void addAssParentIDStatement(Block block) {
		IfStatement ifStatement=ast.newIfStatement();
		addAssParentIDIfExpression(ifStatement);
		addAssParentIDIfThen(ifStatement);
		block.statements().add(ifStatement);
	}

	private void addAssParentIDIfExpression(IfStatement ifStatement) {
		InfixExpression infixExpression=ast.newInfixExpression();
		infixExpression.setOperator(InfixExpression.Operator.NOT_EQUALS);

		MethodInvocation methodInvocation =ast.newMethodInvocation();
		methodInvocation.setName( ast.newSimpleName("getParent"));
		infixExpression.setLeftOperand(methodInvocation);
		infixExpression.setRightOperand(ast.newNullLiteral());

		ifStatement.setExpression(infixExpression);
	}

	private void addAssParentIDIfThen(IfStatement ifStatement)
	{
		Assignment assignment=ast.newAssignment();
		assignment.setLeftHandSide(ast.newSimpleName("parentID"));
		assignment.setOperator(Assignment.Operator.ASSIGN);

		MethodInvocation methodInvocation=ast.newMethodInvocation();
		methodInvocation.setName(ast.newSimpleName("getID"));

		MethodInvocation methodInvocation1=ast.newMethodInvocation();
		methodInvocation1.setName(ast.newSimpleName("getParent"));
		methodInvocation.setExpression(methodInvocation1);

		assignment.setRightHandSide(methodInvocation);

		ExpressionStatement expressionStatement=ast.newExpressionStatement(assignment);
		ifStatement.setThenStatement(expressionStatement);
	}

	private void addSetDataParentIDStatement(Block block)
	{
		MethodInvocation methodInvocation=ast.newMethodInvocation();
		methodInvocation.setName(ast.newSimpleName("set"+parentIdPropName));
		methodInvocation.setExpression(ast.newSimpleName("data"));
		methodInvocation.arguments().add(ast.newSimpleName("parentID"));

		ExpressionStatement expressionStatement=ast.newExpressionStatement(methodInvocation);
		block.statements().add(expressionStatement);
	}

	private void addReturnStatement(Block block)
	{
		ClassInstanceCreation classInstanceCreation=ast.newClassInstanceCreation();
		classInstanceCreation.setType(ast.newSimpleType(ast.newName(accessorType.getFullName())));
		classInstanceCreation.arguments().add(ast.newSimpleName("data"));
		ReturnStatement returnStatement=ast.newReturnStatement();
		returnStatement.setExpression(classInstanceCreation);
		block.statements().add(returnStatement);
	}
}
